<?php
class ControllerModuleNivoSlider extends Controller {
	private $error = array(); 
	
	public function index() {   
		$this->load->language('module/nivo_slider');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('nivo_slider', $this->request->post);		
					
			$this->session->data['success'] = $this->language->get('text_success');
						
			$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		}
				
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_content_top'] = $this->language->get('text_content_top');
		$this->data['text_content_bottom'] = $this->language->get('text_content_bottom');		
		$this->data['text_column_left'] = $this->language->get('text_column_left');
		$this->data['text_column_right'] = $this->language->get('text_column_right');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		
		$this->data['entry_banner'] = $this->language->get('entry_banner');
		$this->data['entry_dimension'] = $this->language->get('entry_dimension'); 
		$this->data['entry_layout'] = $this->language->get('entry_layout');
		$this->data['entry_position'] = $this->language->get('entry_position');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_theme'] = $this->language->get('entry_theme');
		$this->data['entry_featured'] = $this->language->get('entry_featured');
		$this->data['entry_effect'] = $this->language->get('entry_effect');
		$this->data['entry_slices'] = $this->language->get('entry_slices');
		$this->data['entry_box_cols'] = $this->language->get('entry_box_cols');
		$this->data['entry_box_rows'] = $this->language->get('entry_box_rows');
		$this->data['entry_anim_speed'] = $this->language->get('entry_anim_speed');
		$this->data['entry_pause_time'] = $this->language->get('entry_pause_time');
		$this->data['entry_start_slide'] = $this->language->get('entry_start_slide');
		$this->data['entry_direction_nav'] = $this->language->get('entry_direction_nav');
		$this->data['entry_direction_nav_hide'] = $this->language->get('entry_direction_nav_hide');
		$this->data['entry_control_nav'] = $this->language->get('entry_control_nav');
		$this->data['entry_control_nav_thumbs'] = $this->language->get('entry_control_nav_thumbs');
		$this->data['entry_control_nav_thumbs_from_rel'] = $this->language->get('entry_control_nav_thumbs_from_rel');
		$this->data['entry_control_nav_thumbs_search'] = $this->language->get('entry_control_nav_thumbs_search');
		$this->data['entry_control_nav_thumbs_replace'] = $this->language->get('entry_control_nav_thumbs_replace');
		$this->data['entry_keyboard_nav'] = $this->language->get('entry_keyboard_nav');
		$this->data['entry_pause_on_hover'] = $this->language->get('entry_pause_on_hover');
		$this->data['entry_manual_advance'] = $this->language->get('entry_manual_advance');
		$this->data['entry_caption_opacity'] = $this->language->get('entry_caption_opacity');
		$this->data['entry_prev_text'] = $this->language->get('entry_prev_text');
		$this->data['entry_control_next_text'] = $this->language->get('entry_control_next_text');
		$this->data['entry_random_start'] = $this->language->get('entry_random_start');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_module'] = $this->language->get('button_add_module');
		$this->data['button_remove'] = $this->language->get('button_remove');
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->error['dimension'])) {
			$this->data['error_dimension'] = $this->error['dimension'];
		} else {
			$this->data['error_dimension'] = array();
		}
		
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_module'),
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/nivo_slider', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/nivo_slider', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
		$setting = array();
		
		$setting = $this->model_setting_setting->getSetting('nivo_slider');
		
		$this->data['themes'] = array();				
		$this->data['themes'][] = 'default';
		$this->data['themes'][] = 'orman';
		$this->data['themes'][] = 'pascal';
		$this->data['themes'][] = 'nivo';
		
		if (isset($this->request->post['nivo_slider_theme'])) {
			$this->data['nivo_slider_theme'] = $this->request->post['nivo_slider_theme'];
		} elseif (!empty($setting['nivo_slider_theme'])) {
			$this->data['nivo_slider_theme'] = $this->config->get('nivo_slider_theme');
		} else {
			$this->data['nivo_slider_theme'] = 'default';
		}
		
		if (isset($this->request->post['nivo_slider_featured'])) {
			$this->data['nivo_slider_featured'] = $this->request->post['nivo_slider_featured'];
		} elseif (!empty($setting['nivo_slider_featured'])) {
			$this->data['nivo_slider_featured'] = $this->config->get('nivo_slider_featured');
		} else {
			$this->data['nivo_slider_featured'] = 0;
		}
	
		$this->data['effects'] = array();				
		$this->data['effects'][] = 'random';
		$this->data['effects'][] = 'sliceDownRight';
		$this->data['effects'][] = 'sliceDownLeft';
		$this->data['effects'][] = 'sliceUpRight';
		$this->data['effects'][] = 'sliceUpLeft';
		$this->data['effects'][] = 'sliceUpDown';
		$this->data['effects'][] = 'sliceUpDownLeft';
		$this->data['effects'][] = 'fold';
		$this->data['effects'][] = 'fade';
		$this->data['effects'][] = 'boxRandom';
		$this->data['effects'][] = 'boxRain';
		$this->data['effects'][] = 'boxRainReverse';
		$this->data['effects'][] = 'boxRainGrow';
		$this->data['effects'][] = 'boxRainGrowReverse';
		
		
		if (isset($this->request->post['nivo_slider_effect'])) {
			$this->data['nivo_slider_effect'] = $this->request->post['nivo_slider_effect'];
		} elseif (!empty($setting['nivo_slider_effect'])) {
			$this->data['nivo_slider_effect'] = $this->config->get('nivo_slider_effect');
		} else {
			$this->data['nivo_slider_effect'] = 'random';
		}	
		
		if (isset($this->request->post['nivo_slider_slices'])) {
			$this->data['nivo_slider_slices'] = $this->request->post['nivo_slider_slices'];
		} elseif (!empty($setting['nivo_slider_slices'])) {
			$this->data['nivo_slider_slices'] = $this->config->get('nivo_slider_slices');
		} else {
			$this->data['nivo_slider_slices'] = 15;
		}
		
		if (isset($this->request->post['nivo_slider_box_cols'])) {
			$this->data['nivo_slider_box_cols'] = $this->request->post['nivo_slider_box_cols'];
		} elseif (!empty($setting['nivo_slider_box_cols'])) {
			$this->data['nivo_slider_box_cols'] = $this->config->get('nivo_slider_box_cols');
		} else {
			$this->data['nivo_slider_box_cols'] = 8;
		}
		
		if (isset($this->request->post['nivo_slider_box_rows'])) {
			$this->data['nivo_slider_box_rows'] = $this->request->post['nivo_slider_box_rows'];
		} elseif (!empty($setting['nivo_slider_box_rows'])) {
			$this->data['nivo_slider_box_rows'] = $this->config->get('nivo_slider_box_rows');
		} else {
			$this->data['nivo_slider_box_rows'] = 4;
		}
		if (isset($this->request->post['nivo_slider_anim_speed'])) {
			$this->data['nivo_slider_anim_speed'] = $this->request->post['nivo_slider_anim_speed'];
		} elseif (!empty($setting['nivo_slider_anim_speed'])) {
			$this->data['nivo_slider_anim_speed'] = $this->config->get('nivo_slider_anim_speed');
		} else {
			$this->data['nivo_slider_anim_speed'] = 500;
		}
		
		if (isset($this->request->post['nivo_slider_pause_time'])) {
			$this->data['nivo_slider_pause_time'] = $this->request->post['nivo_slider_pause_time'];
		} elseif (!empty($setting['nivo_slider_pause_time'])) {
			$this->data['nivo_slider_pause_time'] = $this->config->get('nivo_slider_pause_time');
		} else {
			$this->data['nivo_slider_pause_time'] = 3000;
		}
		
		if (isset($this->request->post['nivo_slider_start_slide'])) {
			$this->data['nivo_slider_start_slide'] = $this->request->post['nivo_slider_start_slide'];
		} elseif (!empty($setting['nivo_slider_start_slide'])) {
			$this->data['nivo_slider_start_slide'] = $this->config->get('nivo_slider_start_slide');
		} else {
			$this->data['nivo_slider_start_slide'] = 0;
		}
		
		if (isset($this->request->post['nivo_slider_direction_nav'])) {
			$this->data['nivo_slider_direction_nav'] = $this->request->post['nivo_slider_direction_nav'];
		} elseif (!empty($setting['nivo_slider_direction_nav'])) {
			$this->data['nivo_slider_direction_nav'] = $this->config->get('nivo_slider_direction_nav');
		} else {
			$this->data['nivo_slider_direction_nav'] = 1;
		}
		
		if (isset($this->request->post['nivo_slider_direction_nav_hide'])) {
			$this->data['nivo_slider_direction_nav_hide'] = $this->request->post['nivo_slider_direction_nav_hide'];
		} elseif (!empty($setting['nivo_slider_direction_nav_hide'])) {
			$this->data['nivo_slider_direction_nav_hide'] = $this->config->get('nivo_slider_direction_nav_hide');
		} else {
			$this->data['nivo_slider_direction_nav_hide'] = 1;
		}
		
		if (isset($this->request->post['nivo_slider_control_nav'])) {
			$this->data['nivo_slider_control_nav'] = $this->request->post['nivo_slider_control_nav'];
		} elseif (!empty($setting['nivo_slider_control_nav'])) {
			$this->data['nivo_slider_control_nav'] = $this->config->get('nivo_slider_control_nav');
		} else {
			$this->data['nivo_slider_control_nav'] = 1;
		}
		
		if (isset($this->request->post['nivo_slider_control_nav_thumbs'])) {
			$this->data['nivo_slider_control_nav_thumbs'] = $this->request->post['nivo_slider_control_nav_thumbs'];
		} elseif (!empty($setting['nivo_slider_control_nav_thumbs'])) {
			$this->data['nivo_slider_control_nav_thumbs'] = $this->config->get('nivo_slider_control_nav_thumbs');
		} else {
			$this->data['nivo_slider_control_nav_thumbs'] = 0;
		}
		
		if (isset($this->request->post['nivo_slider_control_nav_thumbs_from_rel'])) {
			$this->data['nivo_slider_control_nav_thumbs_from_rel'] = $this->request->post['nivo_slider_control_nav_thumbs_from_rel'];
		} elseif (!empty($setting['nivo_slider_control_nav_thumbs_from_rel'])) {
			$this->data['nivo_slider_control_nav_thumbs_from_rel'] = $this->config->get('nivo_slider_control_nav_thumbs_from_rel');
		} else {
			$this->data['nivo_slider_control_nav_thumbs_from_rel'] = 0;
		}
		
		if (isset($this->request->post['nivo_slider_control_nav_thumbs_search'])) {
			$this->data['nivo_slider_control_nav_thumbs_search'] = $this->request->post['nivo_slider_control_nav_thumbs_search'];
		} elseif (!empty($setting['nivo_slider_control_nav_thumbs_search'])) {
			$this->data['nivo_slider_control_nav_thumbs_search'] = $this->config->get('nivo_slider_control_nav_thumbs_search');
		} else {
			$this->data['nivo_slider_control_nav_thumbs_search'] = '.jpg';
		}
		
		if (isset($this->request->post['nivo_slider_control_nav_thumbs_replace'])) {
			$this->data['nivo_slider_control_nav_thumbs_replace'] = $this->request->post['nivo_slider_control_nav_thumbs_replace'];
		} elseif (!empty($setting['nivo_slider_control_nav_thumbs_replace'])) {
			$this->data['nivo_slider_control_nav_thumbs_replace'] = $this->config->get('nivo_slider_control_nav_thumbs_replace');
		} else {
			$this->data['nivo_slider_control_nav_thumbs_replace'] = '_thumb.jpg';
		}
		
		if (isset($this->request->post['nivo_slider_keyboard_nav'])) {
			$this->data['nivo_slider_keyboard_nav'] = $this->request->post['nivo_slider_keyboard_nav'];
		} elseif (!empty($setting['nivo_slider_keyboard_nav'])) {
			$this->data['nivo_slider_keyboard_nav'] = $this->config->get('nivo_slider_keyboard_nav');
		} else {
			$this->data['nivo_slider_keyboard_nav'] = 1;
		}
		
		if (isset($this->request->post['nivo_slider_pause_on_hover'])) {
			$this->data['nivo_slider_pause_on_hover'] = $this->request->post['nivo_slider_pause_on_hover'];
		} elseif (!empty($setting['nivo_slider_pause_on_hover'])) {
			$this->data['nivo_slider_pause_on_hover'] = $this->config->get('nivo_slider_pause_on_hover');
		} else {
			$this->data['nivo_slider_pause_on_hover'] = 1;
		}
		
		if (isset($this->request->post['nivo_slider_manual_advance'])) {
			$this->data['nivo_slider_manual_advance'] = $this->request->post['nivo_slider_manual_advance'];
		} elseif (!empty($setting['nivo_slider_manual_advance'])) {
			$this->data['nivo_slider_manual_advance'] = $this->config->get('nivo_slider_manual_advance');
		} else {
			$this->data['nivo_slider_manual_advance'] = 0;
		}
		
		if (isset($this->request->post['nivo_slider_caption_opacity'])) {
			$this->data['nivo_slider_caption_opacity'] = $this->request->post['nivo_slider_caption_opacity'];
		} elseif (!empty($setting['nivo_slider_caption_opacity'])) {
			$this->data['nivo_slider_caption_opacity'] = $this->config->get('nivo_slider_caption_opacity');
		} else {
			$this->data['nivo_slider_caption_opacity'] = 0.8;
		}
		
		if (isset($this->request->post['nivo_slider_prev_text'])) {
			$this->data['nivo_slider_prev_text'] = $this->request->post['nivo_slider_prev_text'];
		} elseif (!empty($setting['nivo_slider_prev_text'])) {
			$this->data['nivo_slider_prev_text'] = $this->config->get('nivo_slider_prev_text');
		} else {
			$this->data['nivo_slider_prev_text'] = 'Prev';
		}
		
		if (isset($this->request->post['nivo_slider_next_text'])) {
			$this->data['nivo_slider_next_text'] = $this->request->post['nivo_slider_next_text'];
		} elseif (!empty($setting['nivo_slider_next_text'])) {
			$this->data['nivo_slider_next_text'] = $this->config->get('nivo_slider_next_text');
		} else {
			$this->data['nivo_slider_next_text'] = 'Next';
		}
		
		if (isset($this->request->post['nivo_slider_random_start'])) {
			$this->data['nivo_slider_random_start'] = $this->request->post['nivo_slider_random_start'];
		} elseif (!empty($setting['nivo_slider_random_start'])) {
			$this->data['nivo_slider_random_start'] = $this->config->get('nivo_slider_random_start');
		} else {
			$this->data['nivo_slider_random_start'] = 0;
		}
		
		$this->data['modules'] = array();
		
		if (isset($this->request->post['nivo_slider_module'])) {
			$this->data['modules'] = $this->request->post['nivo_slider_module'];
		} elseif ($this->config->get('nivo_slider_module')) { 
			$this->data['modules'] = $this->config->get('nivo_slider_module');
		}
						
		$this->load->model('design/layout');
		
		$this->data['layouts'] = $this->model_design_layout->getLayouts();

		$this->load->model('design/banner');
		
		$this->data['banners'] = $this->model_design_banner->getBanners();
		
		$this->template = 'module/nivo_slider.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/nivo_slider')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (isset($this->request->post['nivo_slider_module'])) {
			foreach ($this->request->post['nivo_slider_module'] as $key => $value) {
				if (!$value['width'] || !$value['height']) {
					$this->error['dimension'][$key] = $this->language->get('error_dimension');
				}				
			}
		}	
						
		if (!$this->error) {
			return 1;
		} else {
			return 0;
		}	
	}
}
?>